#include "fruit.h"
using namespace cocos2d;
float Fruit::kScale = 0.0;
float Fruit::kScaleX = 0.0;
float Fruit::kScaleY = 0.0;
Fruit *Fruit::create(const char *pszSpriteImageName)
{
	Fruit * fruit = new Fruit();
	fruit->init();
	if(fruit && fruit->initWithSpriteFrameName(pszSpriteImageName))
	{
		fruit->autorelease();
		return fruit;
	}
	CC_SAFE_DELETE(fruit);	
	return NULL;

}
cocos2d::CCRect Fruit::rect()
{
	
	CCPoint point = this->getPosition();
	if(!this->displayFrame())
		return CCRect(0,0,0,0);
	CCSize size = this->displayFrame()->getOriginalSize();
	
	size.width*=Fruit::kScale;
	size.height*=Fruit::kScale;
	CCRect rect = CCRectMake(point.x-size.width/2,point.y-size.height/2,size.width,size.height);
	return rect;
}
bool Fruit::containsTouchLocation(cocos2d::CCPoint point)
{
	return CCRect::CCRectContainsPoint(rect(),point);
}
bool Fruit::containsTouchLocation(cocos2d::CCTouch* touch)
{
	CCPoint point = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
	return CCRect::CCRectContainsPoint(rect(),point);
}
bool Fruit::init()
{
	CCSprite::init();
	CCSize sizewin = CCDirector::sharedDirector()->getWinSize();
	Fruit::kScaleX = sizewin.width/800;
	Fruit::kScaleY = sizewin.height/480;
	Fruit::kScale = kScaleX<kScaleY?kScaleX:kScaleY;
	return true;
}
void Fruit::smallDisapper()
{
	
	CCActionInterval *fade = CCFadeOut::create(0.5);
	this->runAction(CCSequence::create(fade,CCCallFuncN::create(this,callfuncN_selector(Fruit::callBackDisapper)),NULL));
}
void Fruit::callBackDisapper(cocos2d::CCNode * sender)
{
	Fruit * fruit = (Fruit *) sender;
	fruit->setVisible(false);
}